import { queryVoteList, queryVoteStatus, queryPartnerDetail } from '@/services/vote';
import { message } from 'antd';
import router from 'umi/router';
export default {
  namespace: 'vote',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    voteDetail: {},
  },

  effects: {
    *fetchVotelist({ payload }, { call, put }) {
      const response = yield call(queryVoteList, payload);
      console.log(response);
      yield put({
        type: 'save',
        payload: {
          list: response.data.data,
          pagination: {
            total: response.data.total,
            pageSize: 10,
            current: parseInt(response.data.currentPage, 10) || 1,
          },
        },
      });
    },
    *updateVoteStatus({ payload, callback }, { call, put }) {
      const response = yield call(queryVoteStatus, payload);
      if (callback && typeof callback === 'function') {
        callback();
      }
    },
    *fetchVoteDetail({ payload, callback }, { call, put }) {
      const response = yield call(queryPartnerDetail, payload);
      if (response.status === 'success') {
        yield put({
          type: 'saveDetail',
          payload: response.data,
        });
        if (callback && typeof callback === 'function') {
          callback();
        }
      }
    },
  },

  reducers: {
    saveDetail(state, action) {
      return {
        ...state,
        voteDetail: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
